tree grammar EvaluatorWalker;

options {
  language = Java;
  tokenVocab = SampleAST;
  ASTLabelType = CommonTree;
}

@header{ 
 package ast.gen;
 import java.util.Map;
 import java.util.HashMap;
}

@members {
  private Map<String, Integer> variables = new HashMap<String, Integer>();
}

evaluator returns [int result]
  : assigment* e=expression EOF { result = e; }
  ;

assigment
  : ^(':=' IDENT e=expression)
      {variables.put($IDENT.text,e); }
  ;
expression returns [int result]
  : ^('+' op1=expression op2=expression) { result = op1 + op2;}
  | ^('-' op1=expression op2=expression) { result = op1 - op2;}
  | ^('*' op1=expression op2=expression) { result = op1 * op2;}
  | ^('/' op1=expression op2=expression) { result = op1 / op2;}
  | ^('%' op1=expression op2=expression) { result = op1 \% op2;}
  | ^(NEGATION e=expression) { result = -e; }
  | IDENT { 
    
//    result = variables.get();
    if(variables.containsKey($IDENT.text)){
      result = variables.get($IDENT.text);
    }else{
      System.out.println($IDENT.text + " :init to 0");
      result = 0;
    }}
  | INTEGER {result = Integer.parseInt($INTEGER.text);}
  ;
  